package org.example.mrrope.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.mrrope.service.OwnCuttingRopeWorkOrderService;
import org.example.mrrope.utils.DingUtil;
import org.example.mrrope.utils.FormUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

@Service
@Slf4j
public class OwnCuttingRopeWorkOrderServiceImpl implements OwnCuttingRopeWorkOrderService {
    @Autowired
    private DingUtil dingUtil;

    @Override
    public Integer saveOwnCuttingRope(String id) {
        try {
            //得到制结&手工车间派工单数据
            log.info("开始调用宜搭查询接口");
            JSONObject jsonObject = new JSONObject(dingUtil.getYidaById(id));
            log.info("调用查询接口返回切绳车间派工单数据" + jsonObject);
            // body
            int res = 1;
            if (jsonObject.has("tableField_lxifktcx")) {
                JSONArray OwnerTable = jsonObject.getJSONArray("tableField_lxifktcx");
                for (int i = 0; i < OwnerTable.length(); i++) {
                    JSONObject ownJson = getUpJson(jsonObject, id);
                    ownJson.put("employeeField_lxifnhdj", OwnerTable.getJSONObject(i).get("employeeField_lxifktcy_id"));
                    ownJson.put("numberField_lxifnhdq", OwnerTable.getJSONObject(i).get("numberField_lxifktcz"));
                    ownJson.put("dateField_lxifnhdr", OwnerTable.getJSONObject(i).get("dateField_lxifktd0"));
                    ownJson.put("textField_lxin7fu2", "已下发待确认");
                    log.info("*************切绳个人派工单请求参数---->>>>" + ownJson);
                    //陈苗
                    String result = dingUtil.startInstance(ownJson, "28575732551226127", "FORM-FE953C402477449ABAFB54B9578496B7MB8L");//李浩
                    log.info("*************新建切绳个人派工单返回参数---->>>>" + result);
                    if (result != null) {
                        log.info("新建切绳车间个人派工单成功，单号====》" + result);
                        res = 0;
                    } else {
                        log.info("新建制结&手工车间个人派工单失败");
                        res = 1;
                    }
                }
//                String upRes = dingUtil.UpdateYiDa(id, jsonObject);
//                log.info("生成切绳个人任务后更新结果===》" + upRes + jsonObject);
                return res;
            } else {
                log.info("未找到工人");
                return 1;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static JSONObject getUpJson(JSONObject jsonObject, String id) {
        JSONObject ownJson = new JSONObject();
        JSONArray jsonArrayOne = FormUtils.getAssociationFormField("FORM-48B709D2C34A4653AD8D71F2E40F1F27NF9H", jsonObject.get("textField_lxk856z8").toString(), id);
        ownJson.put("associationFormField_lxsn4pm6", jsonArrayOne);
        ownJson.put("textField_lx1mddjk", jsonObject.get("textField_lx1mddjk"));//客户名称
        ownJson.put("textField_lugarhfd", jsonObject.get("textField_lugarhfd"));//半成品B号
        ownJson.put("textField_lugarhfe", jsonObject.get("textField_lugarhfe"));//客人货号
        if (jsonObject.has("imageField_lugarhfm"))
            ownJson.put("imageField_lugarhfm", jsonObject.get("imageField_lugarhfm"));//成品图片
        ownJson.put("textField_lxifktd1", jsonObject.get("textField_lxifktd1"));//产品名称
        ownJson.put("textField_lugarhfx", jsonObject.get("textField_lugarhfx"));//切绳规格重量
        ownJson.put("textField_lugarhg6", jsonObject.get("textField_lugarhg6"));//绳子规格
        ownJson.put("textField_lugarhgb", jsonObject.get("textField_lugarhgb"));//注意点
        return ownJson;
    }
}
